// ==PREPROCESSOR==
// @name "Track Info + Seekbar + Buttons"
// @author "marc2003"
// @import "%fb2k_component_path%samples\complete\js\lodash.min.js"
// @import "%fb2k_component_path%samples\complete\js\helpers.js"
// @import "%fb2k_component_path%samples\complete\js\panel.js"
// @import "%fb2k_component_path%samples\complete\js\seekbar.js"
// ==/PREPROCESSOR==

var colours = {
	buttons : _.RGB(255, 255, 255),
	background : _.RGB(30, 30, 30),
	title : _.RGB(255, 255, 255),
	artist : _.RGB(240, 240, 240),
	time : _.RGB(240, 240, 240),
	seekbar_background : _.RGB(160, 160, 160),
	seekbar_progress : _.RGB(255, 255, 255),
	seekbar_knob : _.RGB(196, 30, 35)
};

var tfo = {
	artist : fb.TitleFormat('%artist%'),
	title : fb.TitleFormat('%title%'),
	playback_time : fb.TitleFormat('%playback_time%  '),
	length : fb.TitleFormat('  %length%')
};

//////////////////////////////////////////////////////////////

var panel = new _.panel();
var seekbar = new _.seekbar(0, 0, 0, 0);
var buttons = new _.buttons();
var img = null;
var bs = _.scale(24);
on_playback_new_track(fb.GetNowPlaying());

buttons.update = function () {
	var y = Math.round((panel.h - bs) / 2);
	this.buttons.stop = new _.button(panel.w - LM - (bs * 8), y, bs, bs, {normal : _.chrToImg(chars.stop, colours.buttons)}, function () { fb.Stop(); }, 'Stop');
	this.buttons.previous = new _.button(panel.w - LM - (bs * 7), y, bs, bs, {normal : _.chrToImg(chars.prev, colours.buttons)}, function () { fb.Prev(); }, 'Previous');
	this.buttons.play = new _.button(panel.w - LM - (bs * 6), y, bs, bs, {normal : !fb.IsPlaying || fb.IsPaused ? _.chrToImg(chars.play, colours.buttons) : _.chrToImg(chars.pause, colours.buttons)}, function () { fb.PlayOrPause(); }, !fb.IsPlaying || fb.IsPaused ? 'Play' : 'Pause');
	this.buttons.next = new _.button(panel.w - LM - (bs * 5), y, bs, bs, {normal : _.chrToImg(chars.next, colours.buttons)}, function () { fb.Next(); }, 'Next');
	this.buttons.console = new _.button(panel.w - LM - (bs * 3), y, bs, bs, {normal : _.chrToImg(chars.console, colours.buttons)}, function () { fb.ShowConsole(); }, 'Console');
	this.buttons.search = new _.button(panel.w - LM - (bs * 2), y, bs, bs, {normal : _.chrToImg(chars.search, colours.buttons)}, function () { fb.RunMainMenuCommand('Library/Search'); }, 'Library Search');
	this.buttons.preferences = new _.button(panel.w - LM - bs, y, bs, bs, {normal : _.chrToImg(chars.preferences, colours.buttons)}, function () { fb.ShowPreferences(); }, 'Preferences');
}

function on_size() {
	panel.size();
	seekbar.x = Math.round(panel.w * 0.22);
	seekbar.w = panel.w - seekbar.x - _.scale(280);
	seekbar.h = _.scale(12);
	seekbar.y = (panel.h - seekbar.h) / 2;
	buttons.update();
}

function on_paint(gr) {
	gr.FillSolidRect(0, 0, panel.w, panel.h, colours.background);
	buttons.paint(gr);
	gr.FillSolidRect(seekbar.x, seekbar.y, seekbar.w + _.scale(6), seekbar.h, colours.seekbar_background);
	if (fb.IsPlaying) {
		if (img) {
			_.drawImage(gr, img, 0, 0, panel.h, panel.h, image.centre);
		}
		gr.GdiDrawText(tfo.title.Eval(), panel.fonts.title, colours.title, panel.h + 10, 0, seekbar.x - panel.h - _.scale(60), panel.h * 0.6, LEFT);
		gr.GdiDrawText(tfo.artist.Eval(), panel.fonts.normal, colours.artist, panel.h + 10, panel.h * 0.3, seekbar.x - panel.h - _.scale(60), panel.h * 0.7, LEFT);
		gr.SetSmoothingMode(2);
		if (fb.PlaybackLength > 0) {
			var pos = seekbar.pos();
			gr.FillSolidRect(seekbar.x, seekbar.y, pos, seekbar.h, colours.seekbar_progress);
			gr.FillSolidRect(seekbar.x + pos, seekbar.y, _.scale(6), seekbar.h, colours.seekbar_knob);
			gr.GdiDrawText(tfo.playback_time.Eval(), panel.fonts.normal, colours.time, seekbar.x - _.scale(45), 0, _.scale(45), panel.h, RIGHT);
			gr.GdiDrawText(tfo.length.Eval(), panel.fonts.normal, colours.time, seekbar.x + seekbar.w + _.scale(6), 0, _.scale(45), panel.h, LEFT);
		}
	}
	gr.DrawRect(seekbar.x, seekbar.y, seekbar.w + _.scale(6), seekbar.h, 1, colours.seekbar_progress);
}

function on_playback_new_track(metadb) {
	if (!metadb) {
		return;
	}
	_.dispose(img);
	img = utils.GetAlbumArtV2(metadb, 0);
	window.Repaint();
}

function on_playback_edited() {
	window.Repaint();
}

function on_playback_seek() {
	seekbar.playback_seek();
}

function on_playback_stop() {
	buttons.update();
	window.Repaint();
}

function on_playback_pause() {
	buttons.update();
	window.Repaint();
}

function on_playback_starting() {
	buttons.update();
	window.Repaint();
}

function on_mouse_wheel(s) {
	if (seekbar.wheel(s)) {
		return;
	}
	if (s == 1) {
		fb.VolumeUp();
	} else {
		fb.VolumeDown();
	}
}

function on_mouse_move(x, y) {
	if (buttons.move(x, y)) {
		return;
	}
	seekbar.move(x, y);
}

function on_mouse_leave() {
	buttons.leave();
}

function on_mouse_lbtn_down(x, y) {
	seekbar.lbtn_down(x, y);
}

function on_mouse_lbtn_up(x, y) {
	if (buttons.lbtn_up(x, y)) {
		return;
	}
	if (seekbar.lbtn_up(x, y)) {
		return;
	}
	fb.RunMainMenuCommand('View/Show now playing in playlist');
}

function on_mouse_rbtn_up(x, y) {
	return panel.rbtn_up(x, y);
}
